package com.ydsungan.web;
import com.ydsungan.bean.Signin;
import com.ydsungan.bean.Teacher;
import com.ydsungan.service.SigninService;
import com.ydsungan.serviceimpl.SigninServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ReleaseServlet extends HttpServlet {
    private SigninService signinService = null;
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public void init() throws ServletException {
        try {
            signinService = new SigninServiceImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        String description = request.getParameter("description");
        String day = request.getParameter("day");
        String time = request.getParameter("time");
        int temperature = Integer.parseInt(request.getParameter("temperature"));
        int address = Integer.parseInt(request.getParameter("address"));
        int contact = Integer.parseInt(request.getParameter("contact"));
        logger.info(description + " : " + day + " : " + time);

        Teacher  teacher = (Teacher) this.getServletContext().getAttribute("loginteacher");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Signin signin = new Signin();
        signin.setReleaseTime(df.format(new Date()));
        signin.setContainsAddress(address);
        signin.setContainsContact(contact);
        signin.setContainsTemp(temperature);
        signin.setSigninDay(day);
        signin.setSigninTime(time);
        signin.setTheme(description);
        signin.setOrganiser(teacher.getId());

        PrintWriter writer = response.getWriter();
        try{
            signinService.addSignin(signin);
            this.getServletContext().setAttribute("signin_has_released","1");

            signin.setSigninTime(signin.getSigninTime().replace("-","至"));
            Date myDate = null;
            String str = "";
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                myDate = sdf.parse(signin.getSigninDay());
                SimpleDateFormat formatter = new SimpleDateFormat("E");
                str = formatter.format(myDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            signin.setSigninDay(signin.getSigninDay()+"("+str+")");
            signin.setReleaseTime(signin.getReleaseTime().split(" ")[1]);
            this.getServletContext().setAttribute("today_signin_content",signin);
            writer.write("{\"code\":\"1\",\"msg\":\"ok\"}");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发布过程出错");
            writer.write("{\"code\":\"0\",\"msg\":\"error\"}");
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
